package com.lex.crossgate.admin.common.utils;

import com.lex.crossgate.admin.modules.sys.entity.SysUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

/**
 * TODO: Shiro工具类
 * @author 李贝留斯
 * @email sunyadongsyd@163.com
 * @date 2018/5/7 17:47
 */
public class ShiroUtils {
	/**
	 * TODO: 获取session
	 * @author 李贝留斯
	 * @email sunyadongsyd@163.com
	 * @date 2018/5/7 17:46
	 */
	public static Session getSession() {
		return SecurityUtils.getSubject().getSession();
	}
	/**
	 * TODO: 获取Subject
	 * @author 李贝留斯
	 * @email sunyadongsyd@163.com
	 * @date 2018/5/7 17:45
	 */
	public static Subject getSubject() {

		return SecurityUtils.getSubject();
	}

	/**
	 * TODO:
	 * @author 李贝留斯
	 * @email sunyadongsyd@163.com
	 * @date 2018/5/7 17:47
	 */
	public static SysUser getUserEntity() {

		return (SysUser)SecurityUtils.getSubject().getPrincipal();
	}

	public static String getUserId() {
		return getUserEntity().getUserId();
	}

	public static void setSessionAttribute(Object key, Object value) {
		getSession().setAttribute(key, value);
	}

	public static Object getSessionAttribute(Object key) {
		return getSession().getAttribute(key);
	}

	public static boolean isLogin() {
		return SecurityUtils.getSubject().getPrincipal() != null;
	}

	public static void logout() {
		SecurityUtils.getSubject().logout();
	}

}
